package cn.edu.scau.cmi.ema.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.Session;


import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;


import cn.edu.scau.cmi.ema.util.CmiSqlUtil;

import java.util.Calendar;

@Repository("EventpunishDAO")
@Transactional
public class EventpunishDAOImpl extends AbstractJpaDao<Eventpunish> implements EventpunishDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Eventpunish.class }));

	@PersistenceContext(unitName = "ema")
	private EntityManager entityManager;

	public EventpunishDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Eventpunish findEventpunishById(Integer id) throws DataAccessException {

		return findEventpunishById(id, -1, -1);
	}

	@Transactional
	public Eventpunish findEventpunishById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findEventpunishById", startResult, maxRows, id);
			return (Eventpunish) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Eventpunish> findEventpunishByName(String name) throws DataAccessException {

		return findEventpunishByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Eventpunish> findEventpunishByName(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEventpunishByName", startResult, maxRows, name);
		return new LinkedHashSet<Eventpunish>(query.getResultList());
	}

	@Transactional
	public Set<Eventpunish> findEventpunishByNameContaining(String name) throws DataAccessException {

		return findEventpunishByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Eventpunish> findEventpunishByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEventpunishByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Eventpunish>(query.getResultList());
	}

	@Transactional
	public Set<Eventpunish> findEventpunishByDate(Calendar date) throws DataAccessException {

		return findEventpunishByDate(date, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Eventpunish> findEventpunishByDate(Calendar date, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEventpunishByDate", startResult, maxRows, date);
		return new LinkedHashSet<Eventpunish>(query.getResultList());
	}

	@Transactional
	public Set<Eventpunish> findEventpunishByDescription(String description) throws DataAccessException {

		return findEventpunishByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Eventpunish> findEventpunishByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEventpunishByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Eventpunish>(query.getResultList());
	}

	@Transactional
	public Set<Eventpunish> findEventpunishByDescriptionContaining(String description) throws DataAccessException {

		return findEventpunishByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Eventpunish> findEventpunishByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEventpunishByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Eventpunish>(query.getResultList());
	}

	@Transactional
	public Set<Eventpunish> findEventpunishByDate2(Calendar date2) throws DataAccessException {

		return findEventpunishByDate2(date2, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Eventpunish> findEventpunishByDate2(Calendar date2, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEventpunishByDate2", startResult, maxRows, date2);
		return new LinkedHashSet<Eventpunish>(query.getResultList());
	}

	@Transactional
	public Eventpunish findEventpunishByPrimaryKey(Integer id) throws DataAccessException {
		return findEventpunishByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Eventpunish findEventpunishByPrimaryKey(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findEventpunishByPrimaryKey", startResult, maxRows, id);
			return (Eventpunish) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Eventpunish> findEventpunishsByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findEventpunishsByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Eventpunish> findAllEventpunishs() throws DataAccessException {

		return findAllEventpunishs(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Eventpunish> findAllEventpunishs(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllEventpunishs", startResult, maxRows);
		return new LinkedHashSet<Eventpunish>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Eventpunish> findEventpunishsByEvent(Integer eventId, int start, int max) throws DataAccessException {
		Query query = createNamedQuery("findAllEventpunishsByEvent", start, max, eventId);
		return new LinkedHashSet<Eventpunish>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Eventpunish> findEventpunishsByPunish(Integer punishId, int start, int max) throws DataAccessException {
		Query query = createNamedQuery("findAllEventpunishsByPunish", start, max, punishId);
		return new LinkedHashSet<Eventpunish>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Eventpunish> findEventpunishByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Eventpunish>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Eventpunish> findEventpunishsByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Eventpunish.class);
		Session session = entityManager.unwrap(Session.class);
		Set<Eventpunish> eventpunishs = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return eventpunishs;
	}

	@Transactional
	public Eventpunish findEventpunishByPhysicalPrimaryKey(Integer eventId, Integer punishId, Calendar date)
			throws DataAccessException {
		return findEventpunishByPhysicalPrimaryKey(eventId, punishId, date, -1, -1);
	}

	@Transactional
	public Eventpunish findEventpunishByPhysicalPrimaryKey(Integer eventId, Integer punishId, Calendar date,
			int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findEventpunishByPhyscialPrimaryKey", startResult, maxRows, eventId,
					punishId, date);
			return (Eventpunish) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Eventpunish entity) {
		return true;
	}
}
